package cn.itcast.interceptor;

import cn.itcast.domain.Admin;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截类
 */

public class PrivilegeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //逻辑：判断用户是否登录  本质：判断session中有没有user
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            //没有登录
            response.sendRedirect(request.getContextPath()+"/pages/login.html");
            return false;
        }
        //放行  访问目标资源
        return true;
    }
}
